package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpadateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller
@Slf4j//lombok的日志注解 添加在类上 酸辣粉fourj
@Tag(name="资讯管理模块")
@RestController//相当于Cotroller @ResponseBody的组合
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    //添加资讯
    @Parameters(value = {
            @Parameter(name="title",description = "标题",required = true),
            @Parameter(name="content",description = "内容",required = true),
            @Parameter(name="type",description = "类型",required = true),
            @Parameter(name="status",description = "状态",required = true),
            @Parameter(name="noticeAddParam",hidden = true)
    })
    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice="+notice);
        noticeMapper.insertNotice(notice);
        return  JsonResult.ok();//没有数据返回的
    }

    //资讯列表
    @Operation(summary = "资讯列表")
    @GetMapping("list")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery) {
        log.debug("noticeAddQuery" + noticeAddQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.list(noticeAddQuery);
        return  JsonResult.ok( noticeListVOS);
    }//有数据返回的

    //资讯详情
    @Operation(summary = "咨询详情")
    @GetMapping("detail")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {
            @Parameter(name = "id",description = "咨询编号",required = true),
            @Parameter(name = "name",description = "名称",required = true,hidden = true)

    })

    public JsonResult detail(Long id,String name) {
        log.debug("id=" + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return  JsonResult.ok(noticeDetailInfoVO);

    }

    //删除资讯
    @Operation(summary = "删除资讯")
    @DeleteMapping("del")
    @ApiOperationSupport(order =40)
    @Parameter(name = "id",description = "咨询编号",required = true)
    public JsonResult del(Long id) {
        log.debug("id=" + id);
        int i = noticeMapper.deleteNoticeById(id);
        if (i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    //更新咨询
    @Operation(summary = "更新资讯")
    @PostMapping("update")
    @ApiOperationSupport(order = 50)
    public JsonResult update(NoticeUpadateParam noticeUpadateParam) {
        log.debug(" noticeUpadateParam=" + noticeUpadateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpadateParam, notice);
        notice.setUserId(1L);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.updateNotice(notice);
        if (i > 0) {
            return  JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}



